// pages/home/home.js

Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		userID: "",
		index: 0,
		date: '2020-01',
		classify: '分类',
		dialog: false,
    chooseDate: false,
    //账单列表
		accounts: [
    ],
		income: 0,
		spending: 0,
		iconClass: {
			娱乐: 'iconfont icon-yule',
			交通: 'iconfont icon-jiaotong',
			食品: 'iconfont icon-lingshi',
			学习: 'iconfont icon-xuexi',
			医疗: 'iconfont icon-yiliao',
			运动: 'iconfont icon-yundong',
			服饰: 'iconfont icon-fushi',
			社交: 'iconfont icon-shejiao',
			数码: 'iconfont icon-shuma',
			美容: 'iconfont icon-meirong',
			工资: 'iconfont icon-gongzi',
			理财: 'iconfont icon-licaishouyi'
		}
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
    this.getList()
		// var ID = app.globalData.userID
    var date_month = this.nowDate_month()
    console.log(date_month)
		//console.log(userID)
		this.setData({
			// userID: ID,
			date: date_month
		});
		// this.getData(ID, date_month).then((result) => {
		// 	//console.log(result)
		// 	this.setData({
		// 		accounts: result
		// 	})
		// 	//console.log(this.data.accounts)
		// 	this.income()
		// 	this.spending()
		// })
	},
  income: function () {
		var incomeList = this.data.accounts.filter(function (a) {
			return a.money > 0
		})
		var income = 0
		for (const m of incomeList) {
			income = income + m['money']
		}
		this.setData({
			income: income
		})
  },
  getList(){
    wx.cloud.callFunction({
      name:"bill",
      data:{
        type:"select"
      }
    }).then(res=>{
      console.log(res)
    this.setData({
      account:res.result.data,
      loading:false
    })
    })
  },
	spending: function () {
		var spendingList = this.data.accounts.filter(function (a) {
			return a.money < 0
		})
		var spending = 0
		for (const m of spendingList) {
			spending = spending + m['money']
		}
		this.setData({
			spending: -spending
		})
	},
//获取当前时间年-月-日
nowDate: function () {
  let now = new Date();
  let _month = (10 > (now.getMonth() + 1)) ? '0' + (now.getMonth() + 1) : now.getMonth() + 1;
  let _day = (10 > now.getDate()) ? '0' + now.getDate() : now.getDate();
  return now.getFullYear() + '-' + _month + '-' + _day;
},
//获取当前时间年-月
nowDate_month: function () {
  let now = new Date();
  let _month = (10 > (now.getMonth() + 1)) ? '0' + (now.getMonth() + 1) : now.getMonth() + 1;
  return now.getFullYear() + '-' + _month;
},

gotoWrite:function(){
  wx.navigateTo({
    url: '../billstype/billstype',
  })

},


bindDateChange: function (e) {
  this.setData({
    date: e.detail.value
  });
  // this.getData(this.data.userID, e.detail.value).then((result) => {
  //   this.setData({
  //     accounts: result
  //   })
  // })
},

  closeChooseClass: function () {
		this.setData({
			dialog: false
		});
  },
  openChooseClass: function () {
		this.setData({
			dialog: true
		});
  },
  chooseClass: function (e) {
    var the_class = e.currentTarget.dataset.text
    console.log(the_class)
    wx.showToast({
      title: the_class,
      duration: 1000,
      mask: true,
      success: (res) => {},
      fail: (res) => {},
    })
	},

})
